package com.meixiang.fragment.account;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.meixiang.R;
import com.meixiang.activity.account.manager.VerifyRealNameActivity;
import com.meixiang.dialog.AlertDialogFragment;
import com.meixiang.global.Config;
import com.meixiang.http.HttpCallBack;
import com.meixiang.http.HttpUtils;
import com.meixiang.main.BaseFragment;
import com.meixiang.tool.Tool;

import org.json.JSONObject;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * 实名认证——查看认证信息
 *
 * @author LZRUI
 */
public class CheckRealNameFragment extends BaseFragment {

    @Bind(R.id.tv_name)
    TextView tvName; // 姓名
    @Bind(R.id.tv_card_num)
    TextView tvCardNum; // 证件号
    @Bind(R.id.tv_service_phone)
    TextView tvServicePhone; // 客服电话

    public CheckRealNameFragment() {

    }

    public static CheckRealNameFragment newInstance() {
        return new CheckRealNameFragment();
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_check_real_name, container, false);
        ButterKnife.bind(this, rootView);
        super.onCreateView(inflater, container, savedInstanceState);
        return rootView;
    }

    @Override
    protected void initTitle() {

    }

    @Override
    protected void initView() {
        tvServicePhone.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (Tool.isFastDoubleClick()) return;
                new AlertDialogFragment.Builder()
                        .setContentText("拨打客服电话")
                        .setRightBtnText("取消")
                        .setLeftBtnText("确认")
                        .setLeftClickCallBack(new AlertDialogFragment.LeftClickCallBack() {
                            @Override
                            public void dialogLeftBtnClick() {
                                Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:" + tvServicePhone.getText().toString().trim()));
                                startActivity(intent);
                            }
                        })
                        .build()
                        .show(getChildFragmentManager(), AlertDialogFragment.TAG);
            }
        });
    }

    @Override
    protected void setData() {
        HttpUtils.post(Config.REALNAME_SELECT, VerifyRealNameActivity.TAG, null, new HttpCallBack(activity) {
            @Override
            public void onSucceed(JSONObject result, String code, String message) {
                String name = "";
                String idCard = "";
                String serviceTell = "";
                try {
                    name = result.getString("name");
                    idCard = result.getString("idCard");
                    serviceTell = result.getString("serviceTell");
                } catch (Exception e) {
                    e.printStackTrace();
                }
                tvName.setText(name);
                tvCardNum.setText(idCard);
                tvServicePhone.setText(serviceTell);
            }

            @Override
            public void onError(String code, String message) {
                if (isVisible()) {
                    Toast.makeText(activity, message, Toast.LENGTH_SHORT).show();
                }
            }
        });
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }

}
